package com.hrms.core;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Role {

	private String name;
	private List<Route> routes;
	private Role role;
	
	public Role(String name, Route ... routes) {
		this(name, null, routes);
	}
	
	public Role(String name,Role role, Route ... routes) {
		this.name = name;
		this.role = role;
		this.routes = Arrays.asList( routes );
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<Route> getRoutes() {
		return routes;
	}
	public void setRoutes(List<Route> routes) {
		this.routes = routes;
	}
	public boolean hasRoute(Route route){
		return routes.contains(route);
	}
	
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	
	public boolean hasRole(Role role1) {
		return hasRole(role1.getName());
	}
	
	public boolean hasRole(String roleName) {
		return name.equals(roleName) || ( this.role!= null ? this.role.getName().equals(roleName) : false );
	}
	
	public boolean hasPagePermission(String page) {
		for(Route r : routes){
			if(r.getPage().equals(page)){
				return true;
			}
		}
		
		if(role!=null){
			for(Route r : role.getRoutes()){
				if(r.getPage().equals(page)){
					return true;
				}
			}
		}
		
		return false;
	}
	
	public List<String> getUserRoles(){
		List<String> userRoles = new ArrayList<String>();
		userRoles.add(getName());
		if(getRole()!=null){
			userRoles.add(getRole().getName());	
		}
		return userRoles;
	}
	
}
